<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>经纪人客户列表</title>
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
    <link href="/lib/css/bootstrap.min.css" rel="stylesheet">
    <link href="/lib/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="/lib/css/style.min.css" rel="stylesheet">
</head>
<style>
    .layui-table{
        width: 100%!important;
    }
</style>

<body>
<div class="container-fluid p-t-15">

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-toolbar clearfix">
                    <form class="pull-right search-bar" method="get" action="#!" role="form">
                        <div class="input-group">
                            <input id="sname" type="text" class="form-control" placeholder="请输入客户姓名">
                            <span class="input-group-btn">
                                <button id="search" class="btn btn-default" type="button">搜索</button>
              </span>
                        </div>
                    </form>
                    <div class="toolbar-btn-action">
                        <a class="btn btn-primary m-r-5" id="add1" href="#!"><i class="mdi mdi-plus"></i> 新增客户</a>
                    </div>
                </div>
                <input type="hidden" th:value="${mid}" id="mid">
            </div>
        </div>

    </div>

</div>
<table id="demo" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<script src="/lib/layui/layui.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script>



<script type="text/javascript" th:inline="none">
    layui.use(['element','jquery','table','layer'], function(){
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var $ = layui.jquery;

        table.render({
            elem: '#demo'
            ,height: 312
            ,url: '/cuslist?name'+"" //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                ,{field: 'name', title: '客户姓名', width:80, align:'left'}
                ,{field: 'sex', title: '性别', width:80, sort: true, align:'left'}
                ,{field: 'tel', title: '电话', width:130, sort: true, align:'left'}
                ,{field: 'email', title: '邮箱', width:164, sort: true, align:'left'}
                ,{fixed: 'right', title: '操作',width: 180, align:'left', toolbar: '#barDemo'}
            ]]
        });
        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            var id = data.id;
            if(layEvent === 'detail'){
                layer.open({
                    type:2,
                    content:'/cusdetail?id='+id,
                    area: ['600px', '400px'],
                })

            } else if(layEvent === 'del'){
                layer.confirm('真的删除经纪人么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    $.ajax({
                        url:"/cusdelete",
                        type:'post',
                        data:{ id:id},
                        datatyp:'json',
                        success:function (message) {
                            if (message.code==0){
                                alert("删除成功！")
                            }else {
                                alert("删除失败！")
                            }
                        }
                    })

                });
            } else if(layEvent === 'edit'){
                layer.open({
                    type: 2,
                    content:'/cusinfo?id='+id,
                    where:{
                        id:data.id
                    },
                    area: ['600px', '400px'],
                    end:function (){
                        table.reload('demo');
                        alert("更新成功")
                    }
                })
            }
        });

        $("#add1").on('click',function () {
            layer.open({
                type:2,
                title:"添加客户",
                content:"/addcus",
                area: ['600px', '400px'],
                end:function (){
                    table.reload('demo');
                }
            })
        });
        $("#search").on('click',function () {
            table.reload('demo',{
                url:'/cuslist'
                ,where:{//条件查询
                    name:$('#sname').val(),
                    id:$("#mid").val()
                }
                ,page:{
                    curr:1
                }
            })
            return false;
        })

    });

</script>
</body>
</html>